package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.PurchaseRequisitionDTO;
import com.eric.domain.po.PurchaseRequisition;
import com.eric.mapper.PurchaseRequisitionMapper;
import com.eric.service.PurchaseRequisitionService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.eric.domain.po.table.PurchaseRequisitionTableDef.PURCHASE_REQUISITION;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class PurchaseRequisitionServiceImpl extends ServiceImpl<PurchaseRequisitionMapper, PurchaseRequisition>  implements PurchaseRequisitionService{

    @Override
    public SaResult saveOnePurchaseRequisition(PurchaseRequisitionDTO purchaseRequisitionDTO) {

        PurchaseRequisition requisition = queryChain().select()
                .where(PURCHASE_REQUISITION.REQUISITION_NUMBER.eq(purchaseRequisitionDTO.getRequisitionNumber()))
                .one();
        boolean empty = BeanUtil.isEmpty(requisition);
        if (empty){
            PurchaseRequisition purchaseRequisition=new PurchaseRequisition();
            BeanUtil.copyProperties(purchaseRequisitionDTO,purchaseRequisition);
            boolean save = save(purchaseRequisition);
            return save ? SaResult.ok() : (SaResult.error("录入错误"));
        }
        else {
            return SaResult.error("该申购单已存在！");
        }
    }

    @Override
    public SaResult updateOne(PurchaseRequisitionDTO purchaseRequisitionDTO) {
        boolean requisitionNameEffective=false;
        boolean requisitionNumberEffective=false;
        boolean requisitionDateEffective=false;
        boolean applicantEffective=false;
        boolean professionalCategoryEffective=false;
        boolean projectNameEffective=false;
        boolean descriptionEffective=false;

        if (purchaseRequisitionDTO.getId()==null){
            return SaResult.error("请选择要修改的申购单！");
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getRequisitionName())){
            requisitionNameEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getRequisitionNumber())){
            requisitionNumberEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getRequisitionDate())){
            requisitionDateEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getApplicant())){
            applicantEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getProfessionalCategory())){
            professionalCategoryEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getProjectName())){
            projectNameEffective=true;
        }
        if (!StrUtil.isBlank(purchaseRequisitionDTO.getDescription())){
            descriptionEffective=true;
        }

//
        /*
        * 通过订购单号，获取到一条数据，记住，一个订购单一条数据，保险起见，应该用list,而不是one
        *
        *
        * */
        List<PurchaseRequisition> requisitionList = queryChain()
                .where(PURCHASE_REQUISITION.REQUISITION_NUMBER
                        .eq(purchaseRequisitionDTO.getRequisitionNumber()))
                .list();
//        如果2个或者2个以上，证明有问题
        if (requisitionList.size()>1){
            return SaResult.error("该申购单已存在！");
        }
       /*
       * 这里还欠缺一个问题，关于与订购单的联合操作问题。申购单号是唯一的，如果申购单号改变了，订购单号中的申购单号也要同步修改。
       * */
        boolean updated = updateChain()
                .set(PURCHASE_REQUISITION.REQUISITION_NAME, purchaseRequisitionDTO.getRequisitionName(), requisitionNameEffective)
                .set(PURCHASE_REQUISITION.REQUISITION_NUMBER, purchaseRequisitionDTO.getRequisitionNumber(), requisitionNumberEffective)
                .set(PURCHASE_REQUISITION.REQUISITION_DATE, purchaseRequisitionDTO.getRequisitionDate(), requisitionDateEffective)
                .set(PURCHASE_REQUISITION.APPLICANT, purchaseRequisitionDTO.getApplicant(), applicantEffective)
                .set(PURCHASE_REQUISITION.PROFESSIONAL_CATEGORY, purchaseRequisitionDTO.getProfessionalCategory(), professionalCategoryEffective)
                .set(PURCHASE_REQUISITION.PROJECT_NAME, purchaseRequisitionDTO.getProjectName(), projectNameEffective)
                .set(PURCHASE_REQUISITION.DESCRIPTION, purchaseRequisitionDTO.getDescription(), descriptionEffective)
                .where(PURCHASE_REQUISITION.ID.eq(purchaseRequisitionDTO.getId()))
                .update();

        return updated? SaResult.ok("修改成功") : SaResult.error("修改失败！");
    }

    @Override
    public SaResult getListByPageService(Integer cur,Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }

        List<PurchaseRequisition> records = queryChain().select()
                .from(PURCHASE_REQUISITION)
                .page(new Page<>(cur, pageSize))
                .getRecords();
        if (!records.isEmpty()){
            List<PurchaseRequisitionDTO> purchaseRequisitionDTOList = BeanUtil.copyToList(records, PurchaseRequisitionDTO.class);
           return SaResult.data(purchaseRequisitionDTOList);
        }


        return SaResult.error("没有数据！");
    }

    @Override
    public SaResult getListCountsService() {
        long count = queryChain().count();
        return SaResult.data(count);
    }
}

